<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-04-30
 * Time: 17:40
 */
namespace app\admin\controller\system;
use app\admin\controller\command\AdminController;
use app\admin\controller\command\ControllerAnnotation;
use app\admin\controller\command\NodeAnotation;
use app\admin\model\system\SystemMenu;
use app\admin\model\traits\Curd;
use app\admin\model\traits\TablHandle;
use think\App;
use think\facade\Db;
use think\Request;

class Menu extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemMenu();
        $array = TablHandle::getClassName(__CLASS__);
        $str = $array[(count($array)-1>=0?count($array)-1:0)];
        if(is_file(__DIR__.DS.'field'.DS.$str.'.txt')){
            $arr = file_get_contents(__DIR__.DS.'field'.DS.$str.'.txt');
            $this->AllData = json_decode($arr,true);
//            echo '<pre>';
//            print_r($this->AllData);
//            exit;
        }else{
            $this->AllData = [
                'field'=>[]
            ];
        }
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index(Request $request)
    {
//        $model = Db::name('system_model')
//            ->where('id',8)
//            ->findOrEmpty();
//        $field = Db::name('system_field')
//            ->where('table',$model['table'])
//            ->select()->toArray();
//        $newfield = [];
//        foreach ($field as $value){
//            if(isset($value['option'])&&$value['option']){
//                $value['option'] = TablHandle::optionToArray($value['option']);
//            }
//            $newfield[$value['field']] = $value;
//        }
//        $model['field'] = $newfield;
//        unset($model['id']);
//        file_put_contents(root_path().'app'.DS.'admin'.DS.'controller'.DS.'system'.DS.'field'.DS.'Menu.txt',json_encode($model,JSON_UNESCAPED_UNICODE));
        if($request->isAjax()){
            $limit = $request->param('limit',10);
            $where = $this->buildTableParames();
            if(empty($where)){
                $data = $this->model->order('sort desc,id asc')->select()->toArray();
            }else{
                $data = $this->model->where($where)->order('sort desc,id asc')->select()->toArray();
            }
            $data = $this->arrangementData($data);
            $res['total'] = count($data);
            $res['data'] = $data;
//            echo '<pre>';
//            print_r($res);
//            exit;
            return json_page_response($res);
        }
        $data = $request->param();
        if(isset($data['id'])&&isset($data['field'])&&$data['id']&&$data['field']){
            $parameter = "?".$data['field'].'='.$data['id'];
        }else{
            $parameter = '';
        }
        $this->assign('parameter',$parameter);
//        echo $parameter;
//        exit;
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add(Request $request)
    {
        $field = $this->AllData['field'];
        if($request->isAjax()){
            $data = $this->SubmitDataArray();
            if(!(isset($data['create_time'])&&$data['create_time'])){
                $data['create_time'] = time();
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
            $bool = $this->model->save($data);
            if($bool){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $id = $request->param('id',null);
        $data = $this->arrangementData();
        $data['pid'] = $id;
        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
//        echo '<pre>';
//        print_r($field);
//        exit;
        $mess = $this->model->getPidMenuList();
        $field['pid']['option'] = $mess;
        $href = Db::name('system_node')
            ->where('status',1)
            ->select()->toArray();
        $field['href']['option'] = $href;
        $table = Db::name('system_model')
            ->where('status',1)
            ->select()->toArray();
        $field['table']['option'] = $table;
        $this->assign('field',$field);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit(Request $request)
    {
        $field = $this->AllData['field'];
        if($request->isAjax()){
            $data = $this->SubmitDataArray();
            if(empty($data['id'])){
                return $this->error('请输入要修改的id');
            }
            if(!(isset($data['update_time'])&&$data['update_time'])){
                $data['update_time'] = time();
            }
            $mo = $this->model->find($data['id']);
//            return $this->error('修改失败',202,$data);
            $bool = $mo->save($data);
            if($bool){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }
        $tiaojian = $request->param();
        $data = $this->model->find($tiaojian['id'])->toArray();
        foreach ($tiaojian as $key=>$value){
            $data[$key] = $value;
        }
        $this->assign('data',$data);
        $field = TablHandle::conversionField($field);
        $mess = $this->model->getPidMenuList();
        $field['pid']['option'] = $mess;
        $href = Db::name('system_node')
            ->where('status',1)
            ->select()->toArray();
        $field['href']['option'] = $href;
        $table = Db::name('system_model')
            ->where('status',1)
            ->select()->toArray();
        $field['table']['option'] = $table;
        $this->assign('field',$field);
        return $this->fetch();
    }
}